package com.springboot.sharding;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.buf.StringUtils;

import java.util.Collection;

/**
 * @Author: TongRui乀
 * @Date: 2019-11-21 14:43
 * @description：   表分片
 */
@Slf4j
public class TableShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {

    public TableShardingAlgorithm(){}

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) {

        log.info("表分片策略[{}]", StringUtils.join(collection,','));

        Integer value = preciseShardingValue.getValue();

        if(value % 2 == 0){
            return "goods_0";
        }else{
            return "goods_1";
        }
    }
}
